package com.dong.web.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class StringUtil {

	public static String replaceBlank(String text) {
		String dest = "";
		if (text != null) {
			return Pattern.compile("\\s*|\t|\r|\n").matcher(text)
					.replaceAll("");
		}
		return dest;
	}

	public static String replaceRn(String text) {
		String dest = "";
		if (text != null) {
			return text.replaceAll("\r|\n", "");
		}
		return dest;
	}

	public static String duplicate(String text, int n) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < n; i++) {
			buffer.append(text);
		}
		return buffer.toString();
	}

	public static String deletedEnd(String valued, String end) {
		if (valued == null || end == null) {
			return valued;
		} else {
			if (valued.endsWith(end)) {
				return valued.substring(0, valued.length() - end.length());
			} else {
				return valued;
			}
		}
	}

	public static List<String> getText(String text) {
		if (text == null) {
			return null;
		} else {
			List<String> rn = new ArrayList<String>();
			Pattern pattern = Pattern.compile("\\$\\{.*?\\}");
			Matcher matcher = pattern.matcher(text);
			while (matcher.find()) {
				String param = matcher.group().replaceAll("\\$\\{|\\}", "");
				rn.add(param.trim());
			}
			return rn;
		}
	}

}
